package com.crtf.kotlin.maven.lombok.java.controller

import com.crtf.kotlin.maven.lombok.java.dto.impl.TunaDTO
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Test

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers

@SpringBootTest
@AutoConfigureMockMvc
class JavaKotlinFishControllerTest {

    @Autowired
    private lateinit var mockMvc: MockMvc

    @Autowired
    private lateinit var objectMapper: ObjectMapper

    @Test
    fun queryFishnet() {
        val builder: MockHttpServletRequestBuilder = MockMvcRequestBuilders.get("/java/fish/fishnet/0")
            .contentType(MediaType.APPLICATION_JSON_VALUE)
            .accept(MediaType.APPLICATION_JSON)
        mockMvc.perform(builder)
            .andExpect(MockMvcResultMatchers.status().isOk)
            .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("200"))
            .andDo(MockMvcResultHandlers.print())
            .andReturn()
    }

    @Test
    fun insertFish() {
        val tunaDTO = TunaDTO(-2)
        val contentJson = objectMapper.writeValueAsString(tunaDTO)
        val builder: MockHttpServletRequestBuilder = MockMvcRequestBuilders.put("/java/fish/fishnet/0")
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .content(contentJson)
        mockMvc.perform(builder)
            .andExpect(MockMvcResultMatchers.status().isOk)
            .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("200"))
            .andDo(MockMvcResultHandlers.print())
            .andReturn()
    }
}